const BaseService = require("../base");

class SpuService extends BaseService {
  async getSpuList({ page, limit }) {
    const ctx = this.ctx;
    const spuList = await ctx.model.Product.Spu.find({}, { spuSaleAttrList: 0, spuImageList: 0 })
      .skip((page - 1) * limit)
      .limit(limit);

    const total = await ctx.model.Product.Spu.countDocuments();
    // 将_id改为id
    return this.toId({
      records: spuList,
      total,
    });
  }

  async saveSpu(spu) {
    const ctx = this.ctx;
    const { spuSaleAttrList, ...rest } = spu;
    // 先存储销售属性
    const saleAttrList = await ctx.model.Product.SpuSaleAttrs.create(spuSaleAttrList);
    // 再存储spu - 此时通过_id建立两个表的联系
    await ctx.model.Product.Spu.create({
      ...rest,
      spuSaleAttrList: saleAttrList.map((saleAttr) => saleAttr._id),
      spuImageList: rest.spuImageList.map((img) => img.id),
    });
    return null;
  }

  async updateSpu({ spuSaleAttrList, ...rest }) {
    const ctx = this.ctx;

    const spu = await ctx.model.Product.Spu.findOne({ _id: rest.id, isAdmin: false });

    if (!spu) return false;
    // spuSaleAttrList有id和没有id
    // 有id代表原来的数据
    const hasIdList = [];
    // 没有id代表新增加的数据
    const noIdList = [];

    spuSaleAttrList.forEach((saleAttr) => {
      if (saleAttr.id) {
        hasIdList.push(saleAttr);
      } else {
        noIdList.push(saleAttr);
      }
    });

    const newNoIdList = await ctx.model.Product.SpuSaleAttrs.create(noIdList);

    let newHasIdList = [];
    for (let i = 0; i < hasIdList.length; i++) {
      const item = hasIdList[i];
      newHasIdList.push(item.id);
      await ctx.model.Product.SpuSaleAttrs.findOneOrUpdate({ _id: item.id }, item);
    }

    spu.spuSaleAttrList = [...newNoIdList.map((saleAttr) => saleAttr._id), ...newHasIdList];
    spu.spuImageList = rest.spuImageList.map((img) => img.id);
    spu.spuName = rest.spuName;
    spu.description = rest.description;
    spu.tmId = rest.tmId;

    await spu.save();

    // await ctx.model.Product.Spu.findOneAndUpdate(
    //   { _id: rest.id },
    //   {
    //     ...rest,
    //     spuSaleAttrList: [...newNoIdList.map((saleAttr) => saleAttr._id), ...newHasIdList],
    //     spuImageList: rest.spuImageList.map((img) => img.id),
    //   }
    // );

    return true;
  }

  async removeSpu(id) {
    const ctx = this.ctx;
    const spu = await ctx.model.Product.Spu.findOne({ _id: id, isAdmin: false });
    if (!spu) return false;
    await ctx.model.Product.SpuSaleAttrs.deleteMany({ _id: { $in: spu.spuSaleAttrList } });
    await ctx.model.Product.ImageList.deleteMany({ _id: { $in: spu.spuImageList } });
    // 还需要删除本地图片-这就不做了，防止用户全部给删了没法恢复
    // 带来的问题就是图片数据会比较庞大-但是除了2021/12/4其他数据都可以删除
    spu.remove();
    return true;
  }

  async getSpuImageList(id) {
    const ctx = this.ctx;
    const spu = await ctx.model.Product.Spu.findOne({ _id: id }, { spuImageList: 1 });
    const imageList = await ctx.model.Product.ImageList.find({ _id: { $in: spu.spuImageList } });
    return this.toId(imageList);
  }
}

module.exports = SpuService;
